package com.example.module_motocircle.model

import android.content.Context
import android.graphics.drawable.Drawable
import androidx.core.content.ContextCompat
import com.example.module_motocircle.R

data class DetailModel(
    val aftersale_result: Any,
    val aftersale_status: Int,
    val aftersale_time: Any,
    val avatar: Any,
    val category_str: String,
    val channel: Int,
    val channel_str: Any,
    val collect_number: Int,
    val comment_number: Int,
    val content: String,
    val create_by: String,
    val create_time: String,
    val delflag: Any,
    val hot_sort: Int,
    val if_aftersale: Int,
    val if_all_sehicle_model: Any,
    val if_all_series: Any,
    val if_audit: Int,
    val if_collect: Int,
    val if_hot: Int,
    val if_like: Int,
    val if_my: Int,
    val if_recommend: Int,
    val if_sensitive: Int,
    val if_tip_off: String,
    val if_transcode: Any,
    val if_up: Int,
    val if_up_str: Any,
    val if_video: Int,
    val image: List<Image>,
    val img_ids: String,
    val lat: Int,
    val like_number: Int,
    val list_show_time: String,
    val location: String,
    val lon: Int,
    val model_name: String,
    val model_nickname: String,
    val posts_id: String,
    val recommend_sort: Int,
    val relation: Int,
    val search_sort: Int,
    val series: String,
    val share_number: Int,
    val tip_off_number: Int,
    val topic_ids: Any,
    val topic_str: Any,
    val topics: List<detailTopic>,
    val update_by: String,
    val update_name: String,
    val update_time: String,
    val user_id: String,
    val vehicle_model_id: String,
    val virtual_like_number: Int,
    val virtual_share_number: Int,
    val weight_score: Double,
    // 是否需要展示关注状态，某些特定页面是不展示的
    var needShowFollowState: Boolean? = null
){
    //显示名称  官方-显示赛科龙官方 其他-显示nikeName
    fun requireName():String{
        return if(isOfficial()){
            "宗申骑士俱乐部官方"
        }else{
            model_nickname?:""
        }
    }
    fun showFollowState(): Boolean {
        if (needShowFollowState == null)
            needShowFollowState = true

        // 非官方 非自己 非关注列表 才需要显示关注关系
        return channel != 1 && needShowFollowState == true && relation != UserRelationEnum.MYSELF.relation
    }
    //显示subName  官方-显示赛科龙官方发布 其他-显示modelName
    fun requireSubName(context: Context): String {
        return if (isOfficial()) {
            "宗申骑士俱乐部官方"
        } else {
            model_nickname
        }
    }
    //显示颜色 官方-显示主题色 其他-显示黑色
    fun requireNameColor(context: Context): Int {
        return if (isOfficial()) {
            ContextCompat.getColor(context, R.color.color_d8000f)
        } else {
            ContextCompat.getColor(context, R.color.color_101010)
        }
    }
    //格式化点赞数显示
    fun formatLikeNumber(): String {
        val total = like_number + virtual_like_number
        return if (total < 10_000) {
            total.toString()
        } else if (total > 10_000) {
            String.format("%sw+", total / 10_000)
        } else {
            "1w"
        }
    }

    //格式化收藏数显示
    fun formatCollectionNumber(): String {
        val total = collect_number
        return if (total < 10_000) {
            total.toString()
        } else if (total > 10_000) {
            String.format("%sw+", total / 10_000)
        } else {
            "1w"
        }
    }

    //格式化分享数显示
    fun formatShareNumber(): String {
        val total = like_number + virtual_like_number
        return if (total < 10_000) {
            total.toString()
        } else if (total > 10_000) {
            String.format("%sw+", total / 10_000)
        } else {
            "1w"
        }
    }

    //格式化评论数
    fun formatCommentNumber(): String {
        return if (comment_number < 10_000) {
            comment_number.toString()
        } else if (comment_number > 10_000) {
            String.format("%sw+", comment_number / 10_000)
        } else {
            "1w"
        }
    }
    fun requireCollectIcon(context: Context): Drawable? {
        return if (if_collect == 1)
            ContextCompat.getDrawable(context, R.mipmap.icon_collection_checked)
        else
            ContextCompat.getDrawable(context, R.mipmap.icon_collection)

    }
    /**
     * 是否官方发布帖子
     */
    fun isOfficial() = channel == 1
    fun showAddIcon(): Boolean {
        return relation <= UserRelationEnum.FANS.relation
    }

    fun showMutualIcon(): Boolean {
        return relation == UserRelationEnum.FRIENDS.relation
    }
    fun followStateName(): String {
        return when (relation) {
            UserRelationEnum.NONE.relation -> {
                "关注"
            }
            UserRelationEnum.FANS.relation -> {
                "关注"
            }
            UserRelationEnum.FOCUS.relation -> {
                "已关注"
            }
            UserRelationEnum.FRIENDS.relation -> {
                "互相关注"
            }
            else -> {
                ""
            }
        }
    }
    //如果为官方发布，需要自己在内容后添加话题
    fun requireContent(): String {
        if (channel == 1 && !topics.isNullOrEmpty()) {
            val sb = StringBuilder()
            sb.append(content)
            topics.forEach {
                sb.append("#${it.topicName}#")
            }
            return sb.toString()
        }
        return content ?: ""
    }
}

data class detailImage(
    val contentLength: Int,
    val contentType: String,
    val filename: String,
    val firstImage: Any,
    val id: Int,
    val imageHeight: Int,
    val imageWidth: Int,
    val thumbnailUrl: String,
    val url: String
)
data class detailTopic(
    val topicId: String?,
    val topicName: String?
)